package com.egene.tw.network;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class ApiHttpClient {
    public static AsyncHttpClient client;

    public ApiHttpClient() {}

    public static void setHttpClient(AsyncHttpClient c) {
        client = c;
    }

    public static AsyncHttpClient getHttpClient() {
        return client;
    }

    public static void cancelAll(Context context) {
        client.cancelRequests(context, true);
    }

    public static void get(String url, AsyncHttpResponseHandler handler) {
        getHttpClient().get(url, handler);
        log(new StringBuilder("GET ").append(url).toString());
    }

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler handler) {
        getHttpClient().get(url, params, handler);
        log(new StringBuilder("GET ").append(url).append("?").append(params).toString());
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler handler) {
        getHttpClient().post(url, params, handler);
        log(new StringBuilder("").append(url).append("?").append(params).toString());
    }

    public static void post(String url, AsyncHttpResponseHandler handler) {
        getHttpClient().post(url, handler);
        log(new StringBuilder("").append(url).append("?").toString());
    }

    /**
     * 获得UserAgent
     *
     * @return
     */
    private static String getUserAgent() {
        StringBuilder ua = new StringBuilder("HSHCLIENT");
        ua.append("/Android");//手机系统平台
        ua.append("/" + android.os.Build.VERSION.RELEASE);//手机系统版本
        ua.append("/" + android.os.Build.MODEL); //手机型号
        return ua.toString();
    }

//    public static RequestParams getPrivateTokenWithParams() {
//        RequestParams params = new RequestParams();
//        return params;
//    }

    private static void log(String log) {
        Log.e("http", log);
    }
}
